package com.wx.spring;

import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.Proxy;

public class RpcConsumerInterFaceFactoryBeanProxy<T> implements FactoryBean<T> {

    public Class<?> targetInterface;
    public String serverName;

    public RpcConsumerInterFaceFactoryBeanProxy(Class<?> targetInterface, String serverName) {
        this.targetInterface = targetInterface;
        this.serverName = serverName;
    }

    @Override
    public T getObject() throws Exception {
        T t = (T) Proxy.newProxyInstance(targetInterface.getClassLoader(), new Class[]{targetInterface}, new JdkProxyInvocationHandler(targetInterface, serverName));
        return t;
    }

    @Override
    public Class<?> getObjectType() {
        return targetInterface.getClass();
    }
}
